<?php

namespace App\Repositories;

use App\Models\Admin;
use App\Models\Network;
use App\Models\NetworkExpress;

class NetworkRepository extends BaseRepository
{
    public function model()
    {
        return Network::class;
    }

    /**
     * 获取网点列表
     * Date: 2018/05/10
     * @param $service_id
     * @param $limit
     * @author Tenny
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getNetworks($service_id = false, $limit = 15)
    {
        $query = \DB::table('network_express as s');
        if ($service_id !== false) {
            $query->where('net.service_id', '=', $service_id);
        }
        $networks = $query
            ->select(['net.designation network_name', 'ex.name as express_name ', 'net.address', 'net.name as owner_name', 'net.cover'])
            ->leftJoin('network as net', 's.network_id', '=', 'net.id')
            ->leftJoin('express_company as ex', 'ex.id', '=', 'net.express_id')
            ->orderBy('net.created_at', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate($limit);
        return $networks;
    }

    /**
     * 获取网点列表
     * Date: 2018/06/10
     * @param $limit
     * @param $service_name
     * @param $status
     * @author Tenny
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getAllNetworks($limit = 15, $service_name = false, $status = false)
    {
        $query = Network::query();
        if ($service_name !== false) {
            $query->where('designation', 'like', '%' . $service_name . '%');
        }
        if ($status !== false) {
            $query->where('status', '=', $status);
        }
        $query->withCount(["services"])
            ->with('express');
        if ($limit === false) {
            $networks = $query->orderBy('created_at', 'desc')->get();
        } else {
            $networks = $query->orderBy('created_at', 'desc')->paginate($limit);
        }
        return $networks;
    }

    /**
     * 根据网点id获取网点详情
     *
     * Date: 2018/05/18
     * @author Tenny
     * @param $network_id
     * @param $service_id
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getNetworkDetails($network_id, $service_id = false)
    {
        $query = Network::query();
        $network = $query
            ->with(["services", "province", "city", "district", "express"])
            ->where('id', $network_id)
            ->orderBy('created_at', 'desc')
            ->first();
        if ($service_id !== false) {
            //查询站点关联网点信息
            $query = NetworkExpress::query();
            $networkExpress = $query
                ->where('network_id', $network_id)
                ->where('service_id', $service_id)
                ->first()
                ->toArray();
            $network->relation = $networkExpress;
        }
        return $network;
    }

    /**
     * 获取关联站点列表
     *
     * Date: 2018/05/18
     * @author Tenny
     * @param $network_id
     * @param $limit
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getRelationStations($network_id, $limit)
    {
        $query = NetworkExpress::query();
        $network = $query
            ->with(["services"])
            ->where('network_id', $network_id)
            ->orderBy('created_at', 'desc')
            ->paginate($limit);
        return $network;
    }

    /**
     * 获取关联站点列表
     *
     * Date: 2018/05/18
     * @author Tenny
     * @param $network_id
     * @param $limit
     * @param $service_name
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getRelationStationLists($network_id, $limit, $service_name = false)
    {
        $query = NetworkExpress::query();
        if ($service_name !== false) {
            $query->where('service.designation', 'like', '%' . $service_name . '%');
        }
        $network = $query
            ->select(['network_express.*', 'service.designation'])
            ->leftJoin('service', 'network_express.service_id', '=', 'service.id')
            ->where('network_express.network_id', $network_id)
            ->orderBy('created_at', 'desc')
            ->paginate($limit);
        return $network;
    }

    /**
     * 获取站点关联网点列表
     *
     * Date: 2018/06/14
     * @author Tenny
     * @param $service_id
     * @param $limit
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getRelationNetworks($service_id, $limit)
    {
        $query = NetworkExpress::query()
            ->with(["services",
                "network" => function ($query) {
                    $query->with('express');
                }
            ])
            ->where('service_id', '=', $service_id)
            ->orderBy('created_at', 'desc');
        if ($limit !== false) {
            $service = $query->paginate($limit);
        } else {
            $service = $query->get();
        }
        return $service;
    }

    /**
     * 获取所有未关联网点列表资源列表
     * Date: 2018/06/10
     * @param $where
     * @param $service_id
     * @author Tenny
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getSelectNetworks($where, $service_id)
    {
        $query = NetworkExpress::query();
        $network_ids = $query
            ->select(['network_express.network_id', 'express_company.express_name'])
            ->leftJoin('network', 'network_express.network_id', '=', 'network.id')
            ->leftJoin('express_company', 'express_company.id', '=', 'network.express_id')
            ->where('network_express.service_id', '=', $service_id)
            ->get()
            ->toArray();
        $ids = $names = [];
        if (is_array($network_ids) && count($network_ids)) {
            foreach ($network_ids as $network) {
                $ids[] = $network['network_id'];
                $names[] = $network['express_name'];
            }
        }
        //根据关联的物流公司查询所有未关联的网点
        $query = Network::query();
        $network_ids = $query
            ->select(['network.id'])
            ->leftJoin('express_company', 'express_company.id', '=', 'network.express_id')
            ->whereIn('express_company.express_name', $names)
            ->get()
            ->toArray();
        if (is_array($network_ids) && count($network_ids)) {
            foreach ($network_ids as $network) {
                $ids[] = $network['id'];
            }
        }
        $ids = array_unique($ids, SORT_REGULAR);
        $query = Network::query();
        if (is_array($where) && count($where) > 0) {
            $query->where($where);
        }
        $query->where('network.status', '=', 1);
        $networks = $query
            ->whereNotIn('id', $ids)
            ->get();
        return $networks;
    }

    /**
     * 判断是否可以关联
     * Date: 2018/06/10
     * @param $service_id
     * @param $network_id
     * @author Tenny
     * @return boolean
     */
    public function isRelation($service_id, $network_id)
    {
        $query = NetworkExpress::query();
        $network_ids = $query
            ->select(['network_express.network_id', 'express_company.express_name'])
            ->leftJoin('network', 'network_express.network_id', '=', 'network.id')
            ->leftJoin('express_company', 'express_company.id', '=', 'network.express_id')
            ->where('network_express.service_id', '=', $service_id)
            ->get()
            ->toArray();
        $names = [];
        if (is_array($network_ids) && count($network_ids)) {
            foreach ($network_ids as $network) {
                $names[] = $network['express_name'];
            }
        }
        if (!is_array($names) && count($names) <= 0) {
            return true;
        }
        $query = Network::query();
        $networks = $query
            ->select(['network.id', 'express_company.express_name'])
            ->leftJoin('express_company', 'express_company.id', '=', 'network.express_id')
            ->where('network.id', '=', $network_id)
            ->first();
        if ($networks) {
            $networks = $networks->toArray();
        }
        if (is_array($networks) && count($networks) > 0 && array_key_exists('express_name', $networks)) {
            if (in_array($networks['express_name'], $names)) {
                return true;
            }
        }
        return false;
    }

    protected function serialization(array $attributes)
    {
        // TODO: Implement serialization() method.
    }
}